package control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BadDetailChartVO;
import model.ChartVO;
import model.InventoryVO;
import model.KimchiService;
import model.OperationInfoVO;
import model.WorkOrderVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class KimchiController extends MultiActionController{
	private KimchiService kimchiService;
	public void setKimchiService(KimchiService kimchiService){
		this.kimchiService=kimchiService;
	}
	
	/**
	 * 다음 작업지시번호 받아오기
	 */
	public ModelAndView getWorkorderNo(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		int workorderNo=kimchiService.getWorkorderNo();
		/*System.out.println(workorderNo);*/
		return new ModelAndView("JsonView","workorderNo",workorderNo);
	}

	
	/**
	 * 재고현황보기
	 */
	public ModelAndView getInventoryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map map=new HashMap();
		int workorderNo=kimchiService.getWorkorderNo();
		map.put("no",workorderNo);
		map.put("list", kimchiService.getInventoryList());
		System.out.println("KimchiController.. getInventoryList.."+kimchiService.getInventoryList());
		return new ModelAndView("JsonView","map",map);
	}
	
	/**
	 * 재료 입고
	 */
	public ModelAndView insertInventory(HttpServletRequest request,
			HttpServletResponse response, InventoryVO ivo) throws Exception{
		kimchiService.insertInventory(ivo);
		System.out.println("KimchiController.. insertInventory.."+ivo);
		return new ModelAndView("JsonView","list",kimchiService.getInventoryList());
	}
	
	/**
	 * 작업요청 현황보기
	 */
	public ModelAndView getWorkOrder(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView","vo",kimchiService.getWorkOrder());
	}
	
	/**
	 * 작업요청
	 */
	public ModelAndView workOrder(HttpServletRequest request,
			HttpServletResponse response, WorkOrderVO wvo) throws Exception{
		kimchiService.workOrder(wvo);
		return new ModelAndView("JsonView","vo",kimchiService.getWorkOrder());
	}
	
	/**
	 * 작업
	 */
	public ModelAndView play(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String station_name=request.getParameter("station_name");
		HashMap map=new HashMap();
		map.put("station_name", station_name);
		map.put("condition_name", request.getParameter("condition_name"));
		map.put("emp_no", 1);
		System.out.println(map);
		kimchiService.play(map);
		
		return getStationInfo(request, response);
	}
	
	
	/**
	 * 각 공정 정보들
	 */
	public ModelAndView getStationInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String station_name=request.getParameter("station_name");
		System.out.println(station_name);
		HashMap map=new HashMap();
		map.put("count", kimchiService.getStationInfoCount(station_name)); // 공정현황 맵
		map.put("errorCount", kimchiService.getStationErrorList(station_name)); // 불량세부현황 리스트
		map.put("productList", kimchiService.getStationProductList(station_name)); // 공정 내 세부 현황 리스트
		System.out.println(map);
		return new ModelAndView("JsonView","info", map);
	}
	/**
	 * 일일 불량 누적 차트 관련 메서드
	 */
	public ModelAndView cumulativeFailureCharts(HttpServletRequest request,
			HttpServletResponse response, ChartVO cvo) throws Exception{
		//페이지 null이면 숫자 1부여
		if(cvo.getPage()==0)
			cvo.setPage(1);
		ArrayList<ChartVO> list=kimchiService.cumulativeFailureCharts(cvo);
		return new ModelAndView("JsonView","list",list);
	}
	/**
	 * 불량 세부정보 상황 차트
	 */
	public ModelAndView badDetailChart(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ArrayList<BadDetailChartVO> list=kimchiService.badDetailChart();
		return new ModelAndView("JsonView","list",list);
	}
	/**
	 * 주간 작업량
	 */
	public ModelAndView workWeek(HttpServletRequest request,
			HttpServletResponse response,ChartVO cvo) throws Exception{
		if(cvo.getPage()==0)
			cvo.setPage(1);
		ArrayList<ChartVO> list=kimchiService.workWeek(cvo);
		return new ModelAndView("JsonView","list",list);
	}

	/**
	 * 가동현황
	 */
	public ModelAndView operationInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HashMap map=new HashMap();
		List<OperationInfoVO> list=kimchiService.operationInfo();
		int quantity=0;
		String color="";
		for(OperationInfoVO vo: list){
			quantity=vo.getQuantity();
			if(quantity>80){
				color="R";
			}else if(quantity>60){
				color="Y";
			}else{
				color="G";
			}
			map.put(vo.getStation_name(), color);
		}
		return new ModelAndView("JsonView","map", map);
	}
	
	/**
	 * 불량품 정보 찾기
	 */
	public ModelAndView getErrorInfoList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		return new ModelAndView("JsonView","list", kimchiService.getErrorInfoList());
	}
	
}
